package com.jt.tenant.conn;

import com.jt.tenant.app.JTApplication;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AD on 2016/5/12.
 */
@HttpInlet(Conn.INLET_GET_NEW_INS_VCI)
public class GetVCIList extends JTAsyGet<GetVCIList.Info> {

    public String token = JTApplication.jtPreferences.readToken();

    public GetVCIList(AsyCallBack<Info> asyCallBack) {

        super(asyCallBack);
    }

    @Override
    protected Info parser(JSONObject object) {
        JSONObject apiTYF = object.optJSONObject("apiTYF");

        JSONObject head = apiTYF.optJSONObject("head");

        Info info = new Info();

        info.msg = head.optString("msg");

        info.code = head.optString("code");

        JSONObject body = apiTYF.optJSONObject("body");

        JSONArray mapList = body.optJSONArray("mapList");

        JSONObject main = mapList.optJSONObject(0);

        JSONObject add = mapList.optJSONObject(1);

        JSONArray MainList = main.optJSONArray("nameList");

        JSONArray AddList = add.optJSONArray("nameList");

        if(MainList!=null){
            for (int i=0;i<MainList.length();i++){

                JSONObject main_item = MainList.optJSONObject(i);

                VCI_ITEM vci_item = new VCI_ITEM();

                vci_item.ID = Tools.F_num(main_item.optString("id"));

                vci_item.NAME = main_item.optString("name");

                vci_item.TYPE = main_item.optString("type");

                if(main_item.optString("type").equals("2")) {

                    JSONArray option = main_item.optJSONArray("priceList");

                    if (option != null) {

                        for (int j = 0; j < option.length(); j++) {

                            JSONObject option_item = option.optJSONObject(j);

                            ITEM item = new ITEM();

                            item.id = Tools.F_num(option_item.optString("id"));

                            item.name = option_item.optString("name");

                            vci_item.ITEMS.add(item);

                        }

                    }
                }

                info.main_list.add(vci_item);
            }
        }

        if(AddList!=null){
            for (int i=0;i<AddList.length();i++){

                JSONObject add_item = AddList.optJSONObject(i);

                VCI_ITEM vci_item = new VCI_ITEM();

                vci_item.ID = Tools.F_num(add_item.optString("id"));

                vci_item.NAME = add_item.optString("name");

                vci_item.TYPE = add_item.optString("type");

                if(add_item.optString("type").equals("2")) {

                    JSONArray option = add_item.optJSONArray("priceList");

                    if (option != null) {

                        for (int j = 0; j < option.length(); j++) {

                            JSONObject option_item = option.optJSONObject(j);

                            ITEM item = new ITEM();

                            item.id = Tools.F_num(option_item.optString("id"));

                            item.name = option_item.optString("name");

                            vci_item.ITEMS.add(item);

                        }

                    }
                }

                info.add_list.add(vci_item);
            }
        }

        return info;
    }

    public static class Info{

        public String msg;

        public String code;

        public List<VCI_ITEM> main_list = new ArrayList<>();

        public List<VCI_ITEM> add_list = new ArrayList<>();

    }
    public static class VCI_ITEM implements Serializable{

        public String ID;

        public String NAME;

        public String TYPE;

        public List<ITEM> ITEMS = new ArrayList<>();

    }

    public static class ITEM implements  Serializable{

        public String name;

        public String id;

    }



}
